package com.burstly.lib.currency;

import android.content.Context;
import com.burstly.lib.BurstlySdk;
import com.burstly.lib.currency.request.CurrencyRequester;
import com.burstly.lib.feature.currency.ICurrencyListener;
import com.burstly.lib.feature.currency.ICurrencyManager;
import com.burstly.lib.network.request.RequestManager;
import com.burstly.lib.util.LoggerExt;
import com.burstly.lib.util.Utils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public final class CurrencyManager
  implements ICurrencyManager
{
  private static final LoggerExt LOG = LoggerExt.getInstance();
  private static final String TAG = "CurrencyManager";
  private static AccountInfo sAccountInfo;
  private static volatile boolean sIninialized;
  private static UserAccountStorage sStorage;
  private final List<ICurrencyListener> mListeners = new ArrayList();
  private final CurrencyRequester mRequester = new CurrencyRequester(this.mListeners);

  private int changeBalance(AccountInfo paramAccountInfo, int paramInt, String paramString, boolean paramBoolean)
    throws IllegalArgumentException
  {
    checkAccountInfo(paramAccountInfo);
    if (paramString == null)
      throw new IllegalArgumentException("Currency name can not be null!");
    UserAccount localUserAccount = sStorage.getAccount(paramAccountInfo, true);
    int i = 0;
    UserCurrencyAccount localUserCurrencyAccount;
    if (localUserAccount != null)
    {
      localUserCurrencyAccount = localUserAccount.getCurrencyAccount(paramString);
      if (localUserCurrencyAccount == null)
      {
        LOG.logInfo("CurrencyManager", "\"{0}\" is not known to the user {1}. Created local currency.", new Object[] { paramString, localUserAccount });
        localUserCurrencyAccount = new UserCurrencyAccount(paramString);
        localUserAccount.setCurrencyAccount(paramString, localUserCurrencyAccount);
      }
      localUserCurrencyAccount.setOldTotalCurrencyAmount(localUserCurrencyAccount.getActualBalance());
      if (!paramBoolean)
        break label146;
      localUserCurrencyAccount.setTotalIncomeAmount(paramInt + localUserCurrencyAccount.getTotalIncomeAmount());
    }
    while (true)
    {
      sStorage.saveAccount(localUserAccount);
      notifyListeners(BalanceUpdateInfo.createFrom(localUserCurrencyAccount));
      i = localUserCurrencyAccount.getActualBalance();
      return i;
      label146: localUserCurrencyAccount.setTotalOutcomeAmount(paramInt + localUserCurrencyAccount.getTotalOutcomeAmount());
    }
  }

  private void checkAccountInfo(AccountInfo paramAccountInfo)
    throws IllegalArgumentException
  {
    checkNotNull(paramAccountInfo, "account info could not be null!");
    checkNotNull(paramAccountInfo.getPubId(), "Publisher id is null");
    checkNotNull(paramAccountInfo.getUserId(), "User id is null");
  }

  private void checkForUpdate(AccountInfo paramAccountInfo)
    throws IOException, IllegalArgumentException
  {
    if (!RequestManager.isOnline())
    {
      LOG.logDebug("CurrencyManager", "No network connection.", new Object[0]);
      throw new IOException("No network connection.");
    }
    checkAccountInfo(paramAccountInfo);
    UserAccount localUserAccount = sStorage.getAccount(paramAccountInfo, true);
    this.mRequester.requestBalanceUpdate(localUserAccount, paramAccountInfo);
  }

  private void checkInitialized()
    throws IllegalStateException
  {
    if (!sIninialized)
      throw new IllegalStateException("CurrencyManager has noot been properly initialized yet! Call for initManager first.");
  }

  private void checkNotNull(Object paramObject, String paramString)
    throws IllegalArgumentException
  {
    if (paramObject == null)
      throw new IllegalArgumentException(paramString);
  }

  private int getBalance(AccountInfo paramAccountInfo, String paramString)
    throws IllegalArgumentException
  {
    checkAccountInfo(paramAccountInfo);
    if (paramString == null)
      throw new IllegalArgumentException("Currency name can not be null!");
    UserAccount localUserAccount = sStorage.getAccount(paramAccountInfo, false);
    if (localUserAccount == null)
      return 0;
    UserCurrencyAccount localUserCurrencyAccount = localUserAccount.getCurrencyAccount(paramString);
    if (localUserCurrencyAccount == null)
    {
      LOG.logInfo("CurrencyManager", "{0} is not known to the user {1}", new Object[] { paramString, localUserAccount });
      return 0;
    }
    return localUserCurrencyAccount.getActualBalance();
  }

  private void internalInitManager(Context paramContext, String paramString1, String paramString2)
  {
    try
    {
      if (!sIninialized)
      {
        Utils.setApplicationContext(paramContext);
        RequestManager.initRquestManager(paramContext);
        UserAccountStorage.initCacheDirectory(paramContext.getApplicationContext());
        sStorage = UserAccountStorage.getInstance();
        BurstlySdk.init(paramContext);
        sIninialized = true;
      }
      if ((paramString2 == null) || (paramString2.length() == 0))
        LOG.logDebug("CurrencyManager", "Using default user ID == device ID", new Object[0]);
      for (sAccountInfo = new AccountInfo(paramString1); ; sAccountInfo = new AccountInfo(paramString1, paramString2))
      {
        UserAccount localUserAccount = sStorage.getAccount(sAccountInfo, true);
        if (localUserAccount.isNew())
        {
          resetBalanceIfNeed(sAccountInfo);
          sStorage.saveAccount(localUserAccount);
        }
        return;
      }
    }
    finally
    {
    }
  }

  private void notifyListeners(BalanceUpdateInfo paramBalanceUpdateInfo)
  {
    if (!this.mListeners.isEmpty())
    {
      HashMap localHashMap = new HashMap();
      localHashMap.put(paramBalanceUpdateInfo.getCurrency(), paramBalanceUpdateInfo);
      Iterator localIterator = this.mListeners.iterator();
      while (localIterator.hasNext())
        ((ICurrencyListener)localIterator.next()).didUpdateBalance(localHashMap);
    }
  }

  private void resetBalance(AccountInfo paramAccountInfo)
  {
    UserAccount localUserAccount = sStorage.getAccount(paramAccountInfo, true);
    if (!localUserAccount.isNew())
      sStorage.clearStorage(localUserAccount.getCacheFilename());
    this.mRequester.requestBalanceReset(paramAccountInfo);
  }

  private void resetBalanceIfNeed(AccountInfo paramAccountInfo)
  {
    if (paramAccountInfo.getUserId().equals(Utils.getDeviceId()))
      resetBalance(sAccountInfo);
  }

  public void addCurrencyListener(ICurrencyListener paramICurrencyListener)
  {
    if (paramICurrencyListener != null)
      this.mListeners.add(paramICurrencyListener);
  }

  public String[] backupFileNames()
    throws IllegalStateException
  {
    checkInitialized();
    return sStorage.getCachedFileList();
  }

  public void checkForUpdate()
    throws IOException, IllegalArgumentException, IllegalStateException
  {
    checkInitialized();
    checkForUpdate(sAccountInfo);
  }

  public int decreaseBalance(int paramInt, String paramString)
    throws IllegalArgumentException, IllegalStateException
  {
    checkInitialized();
    return changeBalance(sAccountInfo, paramInt, paramString, false);
  }

  public int getBalance(String paramString)
    throws IllegalArgumentException, IllegalStateException
  {
    checkInitialized();
    return getBalance(sAccountInfo, paramString);
  }

  public String getUserId()
  {
    if (sIninialized)
      return sAccountInfo.getUserId();
    return null;
  }

  public int increaseBalance(int paramInt, String paramString)
    throws IllegalArgumentException, IllegalStateException
  {
    checkInitialized();
    return changeBalance(sAccountInfo, paramInt, paramString, true);
  }

  public void initManager(Context paramContext, String paramString)
  {
    initManager(paramContext, paramString, null);
  }

  public void initManager(Context paramContext, String paramString1, String paramString2)
  {
    if ((paramString1 != null) && (paramString1.length() > 0))
    {
      internalInitManager(paramContext, paramString1, paramString2);
      return;
    }
    LOG.logError("CurrencyManager", "Publisher or user id should be not null and should not be an empty string. Pulisher id : {0}!", new Object[] { paramString1, paramString2 });
  }

  public void removeAllCurrencyListeners()
  {
    this.mListeners.clear();
  }

  public void removeCurrencyListener(ICurrencyListener paramICurrencyListener)
  {
    if (paramICurrencyListener != null)
      this.mListeners.remove(paramICurrencyListener);
  }
}

/* Location:           E:\android\Toy Defense 2_dex2jar.jar
 * Qualified Name:     com.burstly.lib.currency.CurrencyManager
 * JD-Core Version:    0.6.2
 */